/**
 *******************************************************************************
 * @file  com.h
 * @brief This file contains all the functions prototypes of the communication
 *        device driver.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2019-06-26       Yangjp          First version
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2016, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software is owned and published by:
 * Huada Semiconductor Co., Ltd. ("HDSC").
 *
 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
 *
 * This software contains source code for use with HDSC
 * components. This software is licensed by HDSC to be adapted only
 * for use in systems utilizing HDSC components. HDSC shall not be
 * responsible for misuse or illegal use of this software for devices not
 * supported herein. HDSC is providing this software "AS IS" and will
 * not be responsible for issues arising from incorrect user implementation
 * of the software.
 *
 * Disclaimer:
 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
 * REGARDING THE SOFTWARE (INCLUDING ANY ACCOMPANYING WRITTEN MATERIALS),
 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
 * WARRANTY OF NONINFRINGEMENT.
 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
 * SAVINGS OR PROFITS,
 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
 * FROM, THE SOFTWARE.
 *
 * This software may be replicated in part or whole for the licensed use,
 * with the restriction that this Disclaimer and Copyright notice must be
 * included with each copy of this software, whether used in part or whole,
 * at all times.
 *******************************************************************************
 */
#ifndef __COM_H__
#define __COM_H__

/* C binding of definitions if building with C++ compiler */
#ifdef __cplusplus
extern "C"
{
#endif

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32_common.h"

/**
 * @addtogroup HC32_Boot
 * @{
 */

/**
 * @addtogroup COM
 * @{
 */

/*******************************************************************************
 * Global type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Global pre-processor symbols/macros ('#define')
 ******************************************************************************/
/**
 * @defgroup COM_Global_Macros COM Global Macros
 * @{
 */

/* Baud rate definitions */
#define BR_115200                       (115200UL)
#define BR_256000                       (256000UL)
#define BR_512000                       (512000UL)
#define BR_921600                       (921600UL)
#define BR_1228800                      (1228800UL)

/* Communicaiton device definitions */
#if defined(HC32F460)
    #define PORT_PFSR_FSEL              (0x003FU)
    #define MSTP_FCG1_USART3            (0x04000000UL)

    #define MODEM_USART                 (M4_USART3)
    #define MODEM_USART_CLK             (MSTP_FCG1_USART3)
    #define MODEM_USART_BAUD_RATE       (BR_921600)

    #define MODEM_USART_TX              PFSRE5  // usart_tx PE5
    #define MODEM_USART_RX              PFSRE4  // usart_rx PE4
    #define MODEM_USART_TX_FUNC         (32U)
    #define MODEM_USART_RX_FUNC         (33U)
#elif defined(HC32F120) || defined(HC32M120) || defined(HC32F160)
    #define MODEM_USART_BAUD_RATE       (BR_512000)
    #define MODEM_USART_TX              PCR12   // usart_tx P12
    #define MODEM_USART_RX              PCR11   // usart_rx P11

    #if defined(HC32F120)
        #define MODEM_USART             (M0P_USART1)
        #define MODEM_USART_CLK         (CMU_FCG_UART1)
        #define MODEM_USART_TX_FUNC     (3U << PORT_PCR_FSEL_POS)
        #define MODEM_USART_RX_FUNC     (3U << PORT_PCR_FSEL_POS)
    #elif defined(HC32M120)
        #define MODEM_USART             (M0P_USART2)
        #define MODEM_USART_CLK         (CMU_FCG_UART2)
        #define MODEM_USART_TX_FUNC     (5U << PORT_PCR_FSEL_POS)
        #define MODEM_USART_RX_FUNC     (5U << PORT_PCR_FSEL_POS)
    #elif defined(HC32F160)
        #define MODEM_USART             (CM_USART1)
        #define MODEM_USART_CLK         (CMU_FCG_UART1)
        #define MODEM_USART_TX_FUNC     (3U << GPIO_PCR_FSEL_POS)
        #define MODEM_USART_RX_FUNC     (3U << GPIO_PCR_FSEL_POS)
    #endif
#elif defined(HC32F4A0)
    #define MODEM_USART                 (M4_USART1)
    #define MODEM_USART_CLK             (PWC_FCG3_USART1)
    #define MODEM_USART_BAUD_RATE       (BR_921600)

    #define MODEM_USART_TX              PFSRH15 // usart_tx PH15
    #define MODEM_USART_RX              PFSRH13 // usart_rx PH13
    #define MODEM_USART_TX_FUNC         (32U)
    #define MODEM_USART_RX_FUNC         (33U)
#elif defined(HC32M423)
    #define MODEM_USART                 (CM_USART3)
    #define MODEM_USART_CLK             (CMU_FCG_USART3)
    #define MODEM_USART_BAUD_RATE       (BR_512000)

    #define MODEM_USART_TX              PFSR72  // usart_tx P72
    #define MODEM_USART_RX              PFSR71  // usart_rx P71
    #define MODEM_USART_TX_FUNC         (5U)
    #define MODEM_USART_RX_FUNC         (5U)
#else
    #error "Please select first the target HC32xxxx device used"
#endif

/**
 * @}
 */

/*******************************************************************************
 * Global variable definitions ('extern')
 ******************************************************************************/

/*******************************************************************************
  Global function prototypes (definition in C source)
 ******************************************************************************/
/**
 * @addtogroup COM_Global_Functions
 * @{
 */

void COM_DeInit(void);
void COM_Init(void);
void COM_SendData(uint8_t *u8TxBuff, uint16_t u16Length);
en_result_t COM_RecvData(uint8_t *u8RxBuff, uint16_t u16Length, uint32_t u32Timeout);

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */

#ifdef __cplusplus
}
#endif

#endif /* __COM_H__ */

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
